let vm = new Vue({
    el: '.main-container',  //选中整个main
    data() {
        return {
            role: {},//初始化对象     vue建议声明对象同时进行初始化，避免undefinded
            officeTreeObj: '',
            treeobj: '',
            setting: {
                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "parentId",//父节点的名字
                        idKey: "id" //子节点的名字
                    },//开启简单树
                    key: {
                        title: 'ztree'
                    }
                },
                check: {
                    enable: true,
                    chkboxType: {"Y": "ps", "N": "ps"}
                },
                callback: {//回掉函数
                    onClick: this.onClick
                },
                view: {
                    fontCss: this.fontCss
                }
            },
            nodes: [],
            officeNodes: [],
            params: {
                role: '',
                _resources:[],//原授权资源列表
                resources:[],//当前授权资源列表
                _offices:[], //原授权公司列表
                offices:[] //当前授权公司列表
            }
        }
    },
    methods: {
        update: function () {
            //跟新时获取角色的信息
            this.params.role=this.role;
            //获取新的授权资源id，
            //根据ztree方法获取已经选择按钮的节点
            let resources = this.treeobj.getCheckedNodes(true);
            //删除全部资源这个的节点数据
            if (resources.length>0&&resources[0].id===0){
                resources.splice(0,1);
            }
            //放入全部权限的id数组
            for (let i = 0; i <resources.length ; i++) {
                this.params.resources.push(resources[i].id);
            }
            //授权公司的管理
            if (this.officeTreeObj!=undefined&&this.officeTreeObj!='') {
                let offices = this.officeTreeObj.getCheckedNodes(true);
                //删除全部资源这个的节点数据
                if (offices.length > 0 && offices[0].id === 0) {
                    offices.splice(0, 1);
                }
                //放入全部权限的id数组
                for (let i = 0; i < offices.length; i++) {
                    this.params.offices.push(offices[i].id);
                }
                //隐藏则清空数据
                if (this.role.dataScope != 9) {
                    //添加删除已授权公司的数据
                    this.params._offices = this.params.offices;
                    //不需要添加
                    this.params.offices = [];
                }
            }
            axios.put(
                "/guguanjia/manager/role/update", //相对路径同样受到base标签影响
                this.params,
            ).then(
                response => {  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    let index = parent.layer.getFrameIndex(window.name);//根据当前窗体名字获取其索引值
                    //console.log(index);
                    parent.layer.close(index);//根据索引值关闭子窗口
                    parent.layer.msg("更新成功");//根据索引值关闭子窗口
                }
            ).catch(error => {
                console.log(error.message);
            })
        },
        init: function () {
            axios({
                url: `/guguanjia/manager/menu` //相对路径同样受到base标签影响
            }).then(
                response => {  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    this.nodes = response.data.obj
                    this.nodes[this.nodes.length] = {'id': 0, 'name': '全部权限', 'open': true};
                    $.fn.zTree.init($("#selectTreetreeSelectResEdit"), this.setting, this.nodes);
                    this.treeobj = $.fn.zTree.getZTreeObj("selectTreetreeSelectResEdit");
                    //初始化
                    this.getMenuByRid();
                }
            ).catch(error => {
                console.log(error.message);
            })
            // console.log(tree)
        },
        initOfficeTree: function () {
            // debugger;
            axios({
                url: "/guguanjia/manager/sysOffice" //相对路径同样受到base标签影响
            }).then(
                response => {  //省略function关键字  自动传递上下文中的this
                    //this.apps = response.data.obj.list;
                    this.officeNodes = response.data.obj
                    this.officeNodes[this.officeNodes.length] = {'id': 0, 'name': '全部机构', 'open': true};
                    $.fn.zTree.init($("#selectTreetreeSelectOfficeEdit"), this.setting, this.officeNodes);
                    this.officeTreeObj = $.fn.zTree.init($("#select-treetreeSelectOfficeEdit"), this.setting, this.officeNodes);
                     if(this.officeTreeObj!=''&&this.role.dataScope!=9){
                         this.officeTreeObj = '';
                         $("#treeSelectOfficeEdit").css("display","none");//设置隐藏
                     }else
                    $("#treeSelectOfficeEdit").css("display", "inline-block");
                    //显示当前已经授权的公司
                    this.selectOfficesByRid();
                }
            ).catch(error => {
                console.log(error.message);
            });
            // console.log(tree)
        },
        changeScope: function () {
            //数据范围由9改为其他
            if (this.officeTreeObj != '' && this.role.dataScope != 9) {
                this.officeTreeObj = '';
                $("#treeSelectOfficeEdit").css("display", "none");//设置隐藏
            } else if (this.role.dataScope == 9) {
                //数据范围从其他改为9
                this.initOfficeTree();//初始化公司树   用于跨机构授权处理
            }
        },
        selectOfficesByRid: function () {
            axios({
                url: `manager/sysOffice/selectByRid/${this.role.id}`,
            }).then(response => {
                //将返回的数组节点与全部数组节点进行匹配，找到对应节点进行打钩(checeked)
                let _nodes = response.data.obj;  //已分配的资源

                for (let i = 0; i < _nodes.length; i++) {
                    this.params._offices.push(_nodes[i].id);
                }
                //  旧的节点
                // this.params._offices=_nodes;
                if (this.officeTreeObj!=undefined&&this.officeTreeObj!='') {
                    let transformNodes = this.officeTreeObj.transformToArray(this.officeTreeObj.getNodes());
                    for (let i in _nodes) {
                        for (let j = 0; j < transformNodes.length; j++) {
                            if (_nodes[i].id == transformNodes[j].id) {//找到需要打钩的节点
                                transformNodes[j].checked = true;
                                this.officeTreeObj.updateNode(transformNodes[j]);
                                break;
                            }
                        }
                    }
                }
                // transformNodes = this.officeTreeObj.getNodes();
                // console.log(transformNodes);

            }).catch(function (error) {
                layer.msg(error.message);
            })


        },
        getMenuByRid: function (rid) {
            axios({
                url: `/guguanjia/manager/menu/getResourceByRid/${this.role.id}` //相对路径同样受到base标签影响
            }).then(
                response => {  //省略function关键字  自动传递上下文中的this
                    // debugger;
                    let _nodes = response.data.obj;
                    //旧的权限的id数组
                    for (let i = 0; i < _nodes.length; i++) {
                        this.params._resources.push(_nodes[i].id);
                    }
                    let nodeArray = this.treeobj.transformToArray(this.treeobj.getNodes());
                    for (let i in _nodes) {
                        for (let j in nodeArray) {
                            if (_nodes[i].id == nodeArray[j].id) {
                                nodeArray[j].checked = true;
                                this.treeobj.updateNode(nodeArray[j]);
                                break;
                            }
                        }
                    }
                }
            ).catch(error => {
                console.log(error.message);
            });
        },
        Css: function (treeID, treeNode) {
            return treeNode.highLight ? {'display': 'block'} : {'display': 'none'};
        },
        onClick: function (event, pullDownTreeone, TreeNode) {
            //把公司名回显
            /*this.condition={};
            this.selectPage();*/
            this.name = TreeNode.name;
            //根据公司把公司id设置为当前节点的查询条件
            this.condition.oid = TreeNode.id;
            // console.log(this.condition.oid)
        },
        toSelect: function (id) {
            layer.obj = id;
            layer.open({
                type: 2,
                title: '选择',
                maxmin: true,
                // shadeClose: true, //点击遮罩关闭层
                area: ['100%', '100%'],
                content: 'role/role-select.html',
                end: () => {
                    if (layer.parentName != undefined && layer.parentName != this.role.parentName && layer.parentName.length > 0) {
                        this.role.officeName = layer.parentName;
                        this.role.officeId = layer.aid;
                        // console.log(this.role.officeId)
                    }
                }
            });
        },
        cancelUpdata: function () {
            let index = parent.layer.getFrameIndex(window.name);//根据当前窗体名字获取其索引值
            //console.log(index);
            parent.layer.close(index);//根据索引值关闭子窗口
            parent.layer.msg("更新成功");//根据索引值关闭子窗口
        }
    },
    created: function () {
        this.role = parent.layer.obj;
        this.init();
        this.initOfficeTree();
        // console.log(this.detail.id);
    },
});